package org.yoyo.classroom.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import org.yoyo.classroom.mapper.UserMapper;
import org.yoyo.classroom.security.CustomUserDetails;

import java.util.Collections;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        org.yoyo.classroom.domain.po.User userPO = userMapper.findByUsername(username)
            .orElseThrow(() -> new UsernameNotFoundException("User not found with username: " + username));

        // 创建 CustomUserDetails 对象
        return new CustomUserDetails(
                userPO.getUsername(),
                userPO.getPassword(),
                Collections.emptyList(), // 这里假设用户没有角色，如果有角色需要从 userPO 中获取
                userPO.getId() // 添加 userId
        );
    }
}
